package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DoubleUtil;
import com.ruoyi.system.domain.SysExpenseMerge;
import com.ruoyi.system.domain.SysExpenseStat;
import com.ruoyi.system.domain.SysTenantBacklog;
import com.ruoyi.system.mapper.SysTenantBacklogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysExpenseMapper;
import com.ruoyi.system.domain.SysExpense;
import com.ruoyi.system.service.ISysExpenseService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收租费用Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-06-30
 */
@Service
public class SysExpenseServiceImpl implements ISysExpenseService
{
    @Autowired
    private SysExpenseMapper sysExpenseMapper;

    @Autowired
    private SysTenantBacklogMapper sysTenantBacklogMapper;

    /**
     * 查询收租费用
     * 
     * @param expenseId 收租费用ID
     * @return 收租费用
     */
    @Override
    public SysExpense selectSysExpenseById(Long expenseId)
    {
        return sysExpenseMapper.selectSysExpenseById(expenseId);
    }

    @Override
    public SysExpense selectSysExpenseSum(SysExpense sysExpense) {
        SysExpense expense = sysExpenseMapper.selectSysExpenseSum(sysExpense);
        expense = exchange(expense);
        return expense;
    }

    @Override
    public SysExpense selectSysExpenseByTenantId(Long tenantId) {
        return sysExpenseMapper.selectSysExpenseByTenantId(tenantId);
    }

    /**
     * 查询收租费用列表
     * 
     * @param sysExpense 收租费用
     * @return 收租费用
     */
    @Override
    public List<SysExpense> selectSysExpenseList(SysExpense sysExpense)
    {
        List<SysExpense> list = sysExpenseMapper.selectSysExpenseList(sysExpense);
        for (SysExpense expense : list) {
            expense = exchange(expense);
        }
        return list;
    }

    /**
     * 查询首页统计折线图收租金额
     */
    @Override
    public List<SysExpense> selectSysExpenseStatList() {
        return sysExpenseMapper.selectSysExpenseStatList();
    }

    /**
     * 新增收租费用
     * 
     * @param sysExpense 收租费用
     * @return 结果
     */
    @Override
    public int insertSysExpense(SysExpense sysExpense)
    {
        //新增收租信息
        sysExpense.setCreateTime(DateUtils.getNowDate());
        return sysExpenseMapper.insertSysExpense(sysExpense);
    }

    /**
     * 修改收租费用
     * 
     * @param sysExpense 收租费用
     * @return 结果
     */
    @Override
    public int updateSysExpense(SysExpense sysExpense)
    {
        sysExpense.setUpdateTime(DateUtils.getNowDate());
        return sysExpenseMapper.updateSysExpense(sysExpense);
    }

    /**
     * 删除收租费用对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysExpenseByIds(String ids)
    {
        return sysExpenseMapper.deleteSysExpenseByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收租费用信息
     * 
     * @param expenseId 收租费用ID
     * @return 结果
     */
    @Override
    public int deleteSysExpenseById(Long expenseId)
    {
        return sysExpenseMapper.deleteSysExpenseById(expenseId);
    }

    /**
     * 收租统计查询
     *
     * @param startTime,endTime 收租费用ID
     * @return 收租费用
     */
    @Override
    public List<SysExpenseStat> selectSysExpenseStat(String startTime,String endTime) {
        return sysExpenseMapper.selectSysExpenseStat(startTime,endTime);
    }

    /**
     * 小数点转换
     *
     * @return 结果
     */
    public SysExpense exchange(SysExpense expense){
        if(expense.getMonthlyRentFee()!=null){ expense.setMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getMonthlyRentFee()),2))); }
        if(expense.getRealMonthlyRentFee()!=null){expense.setRealMonthlyRentFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getRealMonthlyRentFee()),2)));}
        if(expense.getPropertyFee()!=null){expense.setPropertyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getPropertyFee()),2)));}
        if(expense.getRealPropertyFee()!=null){expense.setRealPropertyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getRealPropertyFee()),2)));}
        if(expense.getTotalUtilityFee()!=null){expense.setTotalUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getTotalUtilityFee()),2)));}
        if(expense.getWlanFee()!=null){expense.setWlanFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getWlanFee()),2)));}
        if(expense.getTotalMonthRealFee()!=null){expense.setTotalMonthRealFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getTotalMonthRealFee()),2)));}
        if(expense.getApportionRatio()!=null){expense.setApportionRatio(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionRatio()),4)));}
        if(expense.getNinetyPercentRealMonthlyFee()!=null){expense.setNinetyPercentRealMonthlyFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getNinetyPercentRealMonthlyFee()),2)));}
        if(expense.getUseElectricFee()!=null){expense.setUseElectricFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getUseElectricFee()),2)));}
        if(expense.getApportionElectricFee()!=null){expense.setApportionElectricFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionElectricFee()),2)));}
        if(expense.getUseWaterFee()!=null){expense.setUseWaterFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getUseWaterFee()),2)));}
        if(expense.getApportionWaterFee()!=null){expense.setApportionWaterFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionWaterFee()),2)));}
        if(expense.getUseTotalUtilityFee()!=null){expense.setUseTotalUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getUseTotalUtilityFee()),2)));}
        if(expense.getApportionTotalUtilityFee()!=null){expense.setApportionTotalUtilityFee(String.valueOf(DoubleUtil.round(Double.parseDouble(expense.getApportionTotalUtilityFee()),2)));}
        return expense;
    }

}
